<template>
  <!-- 设备类型配置  -->
  <div class="app-container">
    <div class="form-container">
      <el-form ref="filters" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="类型名称">
          <el-input
            v-model="filters.typeName"
            size="small"
            placeholder="请输入类型名称"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <!--      <el-button v-hasPermi="['supervise:workloadConfig:list']" icon="el-icon-plus" :disabled="single" size="mini" @click="handleWorkload">速度设置</el-button>-->
      <el-button v-hasPermi="['supervise:configEnterpType:add']" icon="el-icon-setting" size="middle" :disabled="single" @click="handleConfigType">配置单位类型</el-button>
    </div>
    <!-- 树形表格展示start -->
    <div ref="table">
      <el-table
        ref="multipleTable"
        v-loading="tableLoading"
        :data="treeData"
        :row-style="rowClass"
        stripe
        class="table-list"
        :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height-tree' : 'el-body-page-roll-height-tree') :
          (needTagsView===true ? 'el-body-page-roll-tag-tree' : 'el-body-page-roll-tree')"
        row-key="id"
        :default-expand-all="false"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @selection-change="selectCheck"
        @row-click="rowChecked"
      >
        <el-table-column type="selection" width="38" />
        <el-table-column prop="typeName" header-align="center" label="类型名称" min-width="180" :show-overflow-tooltip="true" />
        <el-table-column prop="orderNum" align="center" label="排序" min-width="40" />
        <!-- <el-table-column prop="orderNum" label="已配单位类型" min-width="120" align="center" /> -->
        <el-table-column prop="remark" :show-overflow-tooltip="true" label="备注" min-width="180" align="center" />
      </el-table>
    </div>
    <!-- 树形表格展示end-->
    <!-- 配置单位类型-->
    <ConfigType ref="ConfigType" />
  </div>
</template>

<script>
import { listProspectType } from '@/api/superviseBaseInfo/typeManage/equipmentType'
// 配置单位类型
import ConfigType from './components/ConfigType'
import { mapState } from 'vuex'
export default {
  components: { ConfigType },
  data() {
    return {
      // 高级设置显示隐藏
      highLevelSet: false,
      // 新增工作量按钮
      single: true,
      // 搜索字段
      filters: {
        typeName: ''
      },
      // 高亮数组
      selectRow: [],
      rowTableData: [],
      treeData: [],
      total: 0,
      // 选中行的id
      multipleSelection: [],
      // 表格加载是否显示等待图标
      tableLoading: false
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    const _this = this// 赋值vue的this
    window.onresize = () => {
      // 调用methods中的事件
      _this.AddTaleData(_this.treeData, _this.total)
    }
    // 跳转路由时关闭窗口提交
    this.$once('hook:beforeDestroy', () => {
      window.onresize = null
    })
  },
  methods: {
    // 加载树形表格
    async getList() {
      this.tableLoading = true
      try {
        const { data: { rows }} = await listProspectType(this.filters)
        this.treeData = rows
        this.rowTableData = []
        this.total = rows.length || 0
        // this.AddTaleData(rows, this.total)
        this.getTableData(this.treeData)
        this.tableLoading = false
      } catch {
        this.tableLoading = false
      }
    },
    // 空白行添加
    AddTaleData(arr, num) {
      const tableHeight = this.$refs.table.offsetHeight
      const row = Math.floor(
        (tableHeight - 36) / 36 // 表格：表头/行高：36px
      )
      for (let i = 0; i < (row - num); i++) {
        this.treeData.push({})
      }
    },
    /* 表格选择事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 选择每行表格前的复选框
    selectCheck(selection) {
      if (selection[0] !== undefined) {
        if (JSON.stringify(selection[0]) === '{}') {
          selection = []
          this.$refs.multipleTable.clearSelection()
        }
      }
      this.multipleSelection = selection
      // 新增工作量按钮
      if (selection.length > 0) {
        this.single = selection[0].parentId !== '0'
      } else {
        this.single = true
      }
      this.multipleSelection = selection
      if (selection.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(selection.pop())
      }
      this.selectRow = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.rowTableData.indexOf(item))
        })
      }
    },
    // 行高亮
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 选择表格前面的对话框时触发的方法
    getTableData(obj) {
      for (var i in obj) {
        this.rowTableData.push(obj[i])
        if (obj[i].children) {
          this.getTableData(obj[i].children)
        }
      }
    },
    /* 表格选择事件 end */

    /* 按钮 start */
    // 搜索方法
    handleQuery() {
      this.getList()
    },
    // 重置
    resetQuery() {
      this.resetForm('filters')
      this.filters = {}
      this.getList()
    },
    // 速度设置
    handleWorkload() {
      this.$router.push({
        path: '/systemConfig/workload',
        query: {
          id: this.multipleSelection[0].id,
          typeName: this.multipleSelection[0].typeName
        }
      })
    },
    // 配置单位类型
    handleConfigType() {
      const { typeNum, id } = this.multipleSelection[0]
      this.$refs.ConfigType.handleAdd('配置单位类型', { typeNum, kind: '1' }, id)
    }
  /* 按钮 end */
  }
}
</script>

<style scoped>
</style>
